﻿
using EvaluationSystem.Dto;
using EvaluationSystem.Models;
using EvaluationSystem.Models.Views;
using EvaluationSystem.Utils;
using EvaluationSystem.ViewModels;

using Microsoft.AspNetCore.Mvc;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.EntityFrameworkCore;

namespace EvaluationSystem.Controllers
{
    //[Route("[controller]/[action]")]
    public class LeaderController : BaseController<LeaderController>
    {
        public LeaderController(ILogger<LeaderController> logger, MyDbContext dbContext) : base(logger, dbContext)
        {
        }

        /// <summary>
        /// 管理员页面
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> Index()
        {
            // 获取用户信息
            GetUserToken<People>(out var people);

            // 获取学生评价情况
            List<Eval_Course> ec_list = await _dbContext.Eval_Course.ToListAsync(); // 学生评分实体
            (List<string> teacher_array, List<float> score_array) = ScoreUtil.getChartData(ec_list);

            // 管理员视图模型
            var vm = new LeaderVM
            {
                People = people,
                TeacherArray = teacher_array,
                ScoreArray = score_array
            };
            // 返回页面
            return View(vm);
        }

        [HttpGet]
        public async Task<IActionResult> ChartData()
        {
            List<Eval_Course> ec_list = await _dbContext.Eval_Course.ToListAsync(); // 学生评分实体
            var tupleValue = ScoreUtil.getChartData(ec_list);
            if (tupleValue != null)
            {
                (List<string> teacher_array, List<float> score_array) = tupleValue;
                return Ok(ResultDto.Ok(data: new
                {
                    teachers = teacher_array,
                    scores = score_array
                }));
            }
            else
            {
                return Ok(ResultDto.Fail(msg: ""));
            }
        }

        #region Student

        [Route("Leader/StuAdd")]
        public IActionResult Student([FromQuery] PageDto pageDto)
        {
            GetUserToken<People>(out var people);

            var pageDataVm = pageDto.GetAutoPageList(people,
                () => _dbContext.People
                    .Where(t => t.Career != null && t.Career == Identity.STUDENT));
            pageDataVm.RouteInfoVM = new RouteInfoVM("StuAdd", "Leader");


            return View(pageDataVm);
        }

        [HttpPost("leader/stu/add")]
        public async Task<IActionResult> AddStudent([FromForm] People person)
        {
            GetUserToken<People>(out var people);

            person.Career = Identity.STUDENT;
            _dbContext.People.Add(person);
            var num = await _dbContext.SaveChangesAsync();

            return Ok(ResultDto.Ok(num > 0 ? "添加成功" : "添加失败"));
        }

        [HttpDelete("leader/stu/delete")]
        public async Task<IActionResult> DeleteStudent([FromQuery] int id)
        {
            if (id > 0)
            {
                _dbContext.People.Remove(new People() { PeopleId = id });
                var num = await _dbContext.SaveChangesAsync();

                return Ok(ResultDto.Ok(num > 0 ? "删除成功" : "删除失败"));
            }
            else
            {
                return Ok(ResultDto.Fail(msg: "参数错误"));
            }
        }

        #endregion

        #region Teacher

        [Route("Leader/TeaAdd")]
        public IActionResult Teacher([FromQuery] PageDto pageDto)
        {
            GetUserToken<People>(out var people);

            var pageDataVm = pageDto.GetAutoPageList(people,
                () => _dbContext.People
                    .Where(t => t.Career != null && t.Career == Identity.TEACHER));
            pageDataVm.RouteInfoVM = new RouteInfoVM("TeaAdd", "Leader");

            return View(pageDataVm);
        }

        [HttpPost("leader/tea/add")]
        public async Task<IActionResult> AddTeacher([FromForm] People person)
        {
            GetUserToken<People>(out var people);

            person.Career = Identity.TEACHER;
            _dbContext.People.Add(person);
            var num = await _dbContext.SaveChangesAsync();

            return Ok(ResultDto.Ok(num > 0 ? "添加成功" : "添加失败"));
        }

        [HttpDelete("leader/tea/delete")]
        public async Task<IActionResult> DeleteTeacher([FromQuery] int id)
        {
            if (id > 0)
            {
                _dbContext.People.Remove(new People() { PeopleId = id });
                var num = await _dbContext.SaveChangesAsync();

                return Ok(ResultDto.Ok(num > 0 ? "删除成功" : "删除失败"));
            }
            else
            {
                return Ok(ResultDto.Fail(msg: "参数错误"));
            }
        }

        #endregion

        #region Notify

        [Route("Leader/Notify")]
        public IActionResult Notify([FromQuery] PageDto pageDto)
        {
            GetUserToken<People>(out var people);

            var pageDataVm = pageDto.GetAutoPageList(people,
                () => _dbContext.Notifies.Include(t => t.Leader));
            pageDataVm.RouteInfoVM = new RouteInfoVM("Notify", "Leader");

            return View(pageDataVm);
        }

        [HttpPost("leader/notify/add")]
        public async Task<IActionResult> AddNotify([FromForm] string title, [FromForm] string content)
        {
            GetUserToken<People>(out var people);

            var notify = new Notify
            {
                Title = title,
                Content = content,
                CreatedAt = DateTime.Now,
                LeaderId = people.PeopleId
            };

            _dbContext.Notifies.Add(notify);
            var num = await _dbContext.SaveChangesAsync();

            return Ok(ResultDto.Ok(num > 0 ? "添加成功" : "添加失败"));
        }

        [HttpDelete("leader/notify/delete")]
        public async Task<IActionResult> DeleteNotify([FromQuery] int id)
        {
            if (id > 0)
            {
                _dbContext.Notifies.Remove(new Notify() { NotifyId = id });
                var num = await _dbContext.SaveChangesAsync();

                return Ok(ResultDto.Ok(num > 0 ? "删除成功" : "删除失败"));
            }
            else
            {
                return Ok(ResultDto.Fail(msg: "参数错误"));
            }
        }

        #endregion
    }
}
